package com.szq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.szq.common.CustomException;
import com.szq.entity.Category;
import com.szq.entity.Dish;
import com.szq.entity.Setmeal;
import com.szq.service.CategoryService;
import com.szq.mapper.CategoryMapper;
import com.szq.service.DishService;
import com.szq.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 吾空白
* @description 针对表【category(菜品及套餐分类)】的数据库操作Service实现
* @createDate 2023-04-10 14:13:44
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除分类，删除之前需要进行判断
     * @param id
     */
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count = (int)dishService.count(dishLambdaQueryWrapper);
        //查询当前分类是否关联了菜品，如果已经关联，抛出业务异常
       if (count>0){
           //已经关联菜品，抛出异常
           throw new CustomException("当前分类下关联了菜品，不能删除");

       }
       LambdaQueryWrapper<Setmeal> setmealServiceLambdaQueryWrapper=new LambdaQueryWrapper<>();
       setmealServiceLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count1 = (int)setmealService.count(setmealServiceLambdaQueryWrapper);
        //查询当前分类是否关联了套餐，如果已经关联，抛出业务异常
        if (count1>0){
            //已经关联了套餐，抛出异常
            throw new CustomException("当前分类下关联了套餐，不能删除");
        }
        //正常删除分类
        super.removeById(id);
    }
}




